//
//  ZZVisitorView.swift
//  web
//
//  Created by 张卓 on 2017/1/17.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZVisitorView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    //1. 定义闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置方可视图信息
  
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    //供外界使用的方法
//    func setupVisiorViewInfo(imageName:String?,title:String?){
//        if let img = imageName,let tit = title{
//            //重新赋值
//            iconImageView.image = UIImage(named: img)
//            massageLabel.text = tit
//            feedImageView.isHidden = true
//        }else {//说明是首页
//            setupFeedImageViewAnim()
//            
//        }
//    }
    // MARK: - 供外界设置的方法
    func setupVisiorViewInfo(imgName: String?, title: String?){
        // 代表是消息 发现 我
        if let img = imgName, let tit = title {
            // 重新赋值
            iconImageView.image = UIImage(named: img)
            massageLabel.text = tit
            // 隐藏圆圈图片
            feedImageView.isHidden = true
        }else {
            // 代表是首页
            // 执行动画
            setupFeedImageViewAnim()
        }
    }
    //设置圆圈动画
    func setupFeedImageViewAnim(){
//        let anim = CABasicAnimation(KeyPath: "transform.rotation")
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //当切换控制器 或 程序退到后台, 默认动画会被移除
        anim.isRemovedOnCompletion = false
        feedImageView.layer.add(anim, forKey: nil)
        
    }
    
    
     private func setupUI(){
        
        self.backgroundColor = RGB(r: 235, g: 235, b: 235)
        
//        feedImageView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
//        
        
        //1 添加空间
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(massageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //2. 添加约束
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        massageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.width.equalTo(230)
        }
        loginButton.snp_makeConstraints { (make) in
            make.left.equalTo(massageLabel)
            make.top.equalTo(massageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(loginButton)
            make.top.equalTo(loginButton)
            make.right.equalTo(massageLabel)
        }
        
        
    }
    
    //懒加载控件
    // 圆圈图片
    private lazy var feedImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var maskImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    private lazy var massageLabel:UILabel = {
       let label = UILabel(fontSize: ZZNORMALFONTSIZE, textColor: UIColor.darkGray, text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
//        label.numberOfLines = 0
//        //对齐方式
//        label.textAlignment = .center

        return label
    }()
    //登录按钮
    private lazy var loginButton :UIButton = {
       let bnt = UIButton()
        //背景图
        bnt.setBackgroundImage(UIImage(named:"common_button_white"), for: UIControlState.normal)
        //设置文字颜色
        bnt.setTitle("登录", for: UIControlState.normal)
        bnt.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        bnt.setTitleColor(ZZThemeColor, for: UIControlState.highlighted)
        //字体大小
        bnt.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        bnt.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        
        return bnt
        
    }()
    private lazy var registerButton :UIButton = {
        let bnt = UIButton()
        //背景图
        bnt.setBackgroundImage(UIImage(named:"common_button_white"), for: UIControlState.normal)
        //设置文字颜色
        bnt.setTitle("注册", for: UIControlState.normal)
        bnt.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        bnt.setTitleColor(ZZThemeColor, for: UIControlState.highlighted)
        //字体大小
        bnt.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        bnt.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        return bnt
        
    }()
    
    @objc private func buttonClick() {
        //3 执行闭包
        closure?()
        
    }
    
    
}
